package com.longge;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-05-03 00:42
 */
public class MinDepth {

    public int minDepth(TreeNode root) {
        if (root==null) return 0;
        Deque<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int h = 1;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode treeNode = queue.poll();
                if (treeNode.left == null && treeNode.right == null) return h;
                if (treeNode.left != null) queue.offer(treeNode.left);
                if (treeNode.right != null) queue.offer(treeNode.right);
            }
            h++;
        }
        return h;
    }
}
